package XML;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class XsltTrans {

    public static void transformXmlByXslt(String srcXml, String dstXml, String xslt) {

        // 获取转换器工厂
        TransformerFactory tf = TransformerFactory.newInstance();

        try {
            // 获取转换器对象实例
            Transformer transformer = tf.newTransformer(new StreamSource(xslt));
            // 进行转换
            transformer.transform(new StreamSource(srcXml), new StreamResult(new FileOutputStream(dstXml)));

        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    private static volatile Long isneed = 0L;

    public static void main(String[] args) throws Exception {
        String srcXml = "D:\\WSpaces\\idea_work\\Z-Utils\\src\\XML\\oldAttr3.xml";
        String dstXml = "D:\\WSpaces\\idea_work\\Z-Utils\\src\\XML\\new.xml";
        String xslt = "D:\\WSpaces\\idea_work\\Z-Utils\\src\\XML\\modul5.xslt";

        String dest = transXmlByXsl(srcXml,xslt,null);
         System.out.println(dest);


        // transformXmlByXslt(srcXml, dstXml, xslt);

 //       ExecutorService executorService = Executors.newFixedThreadPool(20);

//        for (int i = 0; i < 20; i++) {
//
//            executorService.execute(()->{
//
//                for (int j = 0; j < 10; j++) {
//                    try {
//                        long startTime = System.currentTimeMillis();
//                        String dest = transXmlByXsl(srcXml,xslt,null);
//                      //  System.out.println(dest);
//                        long endTime = System.currentTimeMillis();
//
//                        synchronized (Object.class) {
//                            isneed += (endTime - startTime);
//                        }
//
//                        System.out.println("耗时："+isneed + "毫秒");
//
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//
//
//        }

//        executorService.shutdown();

    }



    public static String transXmlByXsl(String xmlPath, String xsltPath, String encoding) throws Exception{
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        Result destResult = new StreamResult(printWriter);

        Source xmlSource = null;
		Source xsltSource = null;
        if(encoding != null){
//          srcSource = new StreamSource(new ByteArrayInputStream(xml.getBytes(encoding)));
//			xslSource = new StreamSource(new ByteArrayInputStream(xsl.getBytes(encoding)));
            xmlSource = new StreamSource(xmlPath);
            xsltSource = new StreamSource(xsltPath);
        } else {
//          srcSource = new StreamSource(new ByteArrayInputStream(xml.getBytes()));
//			xslSource = new StreamSource(new ByteArrayInputStream(xsl.getBytes()));
            xmlSource = new StreamSource(xmlPath);
            xsltSource = new StreamSource(xsltPath);
        }

        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);
        trans.transform(xmlSource, destResult);

        String parsedXml = stringWriter.toString();

        printWriter.close();
        return parsedXml;

    }


}
